<?php
/**
 *
 * @author 孙春雷
 *        
 */
class Model_Deliveryorders extends Modelnewerp {
	
	// 数据库字段变量
	const delivery_date = 'delivery_date';

	protected $id = 'delivery_id';
	const order_id = 'order_id';
	const delivery_department = 'delivery_department';
	const delivery_man = 'delivery_man';
	const sku = 'sku';
	const sku_name = 'sku';
	const sku_standard = 'sku_standard';
	const storage_position = 'storage_position';
	const quantity = 'quantity';
	const single_price = 'single_price';
	const money = 'money';
	const cost = 'cost';
	const internationalship_cost = 'internationalship_cost';
	const remark = 'remark';
	const type = 'type';

	/**
	 * 调用的数据表
	 *
	 * @var unknown
	 */
	protected $_table = 'deliveryorders';

	/**
	 * 获取出库的成本和数量
	 *
	 * @param unknown $starttime        	
	 * @param unknown $endtime        	
	 * @param string $sku        	
	 * @return Ambigous <multitype:, unknown, NULL>
	 */
	public function getCountAndCost($starttime, $endtime, $sku = NULL)
	{
		// 定义结果数组
		$result = array ();
		
		// 统计出库的数量和成本
		$sql = DB::select( array (
				self::sku,
				'sku' 
		), array (
				DB::expr( "sum(" . self::quantity . ")" ),
				'count' 
		), array (
				DB::expr( 0 ),
				'cost' 
		) )->from( $this->_table )
			->where( self::delivery_date, '>=', $starttime )
			->where( self::delivery_date, '<', $endtime  . ' 23:59:59' );
		if ($sku)
		{
			$sql->where( self::sku, '=', $sku );
		}
		$sql->group_by( self::sku );
		$current_out = $sql->execute( $this->_db )
			->as_array();
		// 格式化数组
		foreach ( $current_out as $c )
		{
			$sku_value = strtoupper( $c ['sku'] );
			$result [$sku_value] ['count'] = $c ['count'];
			$result [$sku_value] ['cost'] = round($c ['cost'],2);
		}
		
		return $result;
	}

	/**
	 * 根据sku更新成本
	 *
	 * @param unknown $cost        	
	 * @param unknown $sku        	
	 * @return Ambigous <object, mixed, number, Database_Result_Cached, multitype:>
	 */
	public function modifyCost($cost, $sku, $starttime, $endtime)
	{
		$sql = "update $this->_table set " . self::cost . "=$cost*" . self::quantity . " where sku=$sku and " . self::delivery_date . ">=$starttime and " . self::delivery_date . "<=$endtime";
		return DB::query( Database::UPDATE, $sql );
	}
	/**
	 * 获取60天的销量
	 *
	 * @param unknown $starttime
	 * @param unknown $endtime
	 * @author chamlinsun@qq.com
	 */
	public static function getSaleAmount($starttime, $endtime, $sku, $warehouseid, $supplier_id){
	    $result = '';
	    // 统计出库的销量
	    $sql = 'SELECT sum(quantity) as count
                 FROM deliveryorders
                 WHERE type=10';
	    !empty($starttime) && $sql .= " and delivery_date >'".$starttime."'";
	    !empty($endtime) && $sql .= " and delivery_date <'".$endtime."'";
	    !empty($warehouseid) && $sql .= " and warehouseid =".$warehouseid;
	    !empty($supplier_id) && $sql .= " and supplier_id =".$supplier_id;
	    !empty($sku) && $sql .= " and sku ='".$sku."'";
	    
	    $current_out = DB::query(Database::SELECT,$sql.$where)->execute('newerp')->current('count');
	    
	    return $current_out['count'];
	}
}
